package org.youyun56.gpsserver.command;

import org.apache.log4j.Logger;
import org.apache.mina.core.session.IoSession;
import org.youyun56.gpsserver.Task;
import org.youyun56.gpsserver.common.Page;
import org.youyun56.gpsserver.common.YouyunMap;

import com.sun.net.httpserver.HttpExchange;

public enum Command implements CommandType {

    LOGIN(CommandType.Login, LoginExecutor.class), EXIT(CommandType.Exit, ExitExecutor.class), AddGood(CommandType.AddGood, AddGoodExecutor.class),
    GetGoods(CommandType.GetGoods, GetGoodExecutor.class), ADDIM(CommandType.ADD_IM, AddImExecutor.class),
    UserOut(CommandType.USER_OUT, LogoutExecutor.class), ChangeCarHearvy(CommandType.Change_Car_Hearvy, ChangeCarHearvyExecutor.class),
    UpdateCarStatus(CommandType.Update_Car_Status, UpdateCarStatus.class),
    ChangeCurrentCity(CommandType.Change_Current_City, ChangeCurrentCity.class), AppendGood(CommandType.Append_Good, AppendGoodExecutor.class),
    AppendGoodEnd(CommandType.Append_Good_END, AppendGoodEndExecutor.class), GoodSearch(CommandType.Good_Search, GoodSearchExecutor.class),
    GoodSearchEnd(CommandType.Good_Search_END, GoodSearchEndExecutor.class),
    UploadCarGpsLocation(CommandType.UploadLocation, UploadCarGpsLocationExecutor.class), ReConnect(CommandType.Re_Connect, ReConnectExecutor.class);
    private Integer id;
    private Class<? extends Task> clazz;
    private YouyunMap parameters;
    private String input;
    private Object output;
    private final static String AND = "&";
    private final static String equals = "=";
    private IoSession session;
    private HttpExchange exchange;
    private final static Logger log = Logger.getLogger(Command.class);
    private Integer total;
    private Page page;

    private Command(Integer id, Class<? extends Task> clazz) {
        this.id = id;
        this.clazz = clazz;
    }

    public static Task getExecute(String input) {
        try {
            YouyunMap parameters = new YouyunMap();
            String[] kvs = input.split(AND);
            for (int i = 0; i < kvs.length; i++) {
                String kv[] = kvs[i].split(equals);
                if (kv.length != 2)
                    continue;
                parameters.put(kv[0].trim(), kv[1].trim());
            }
            int id = Integer.parseInt(parameters.get("id"));
            Command command = getCommand(id);
            if (command != null) {
                command.setInput(input);
                command.setParameters(parameters);
                Task task = command.getClazz().newInstance();
                task.setCommand(command);
                return task;
            } else {
                log.error(String.format("Command id %d not found", id));
                return null;
            }
        }
        catch (Exception e) {
            // TODO: handle exception
            log.error("get execute exception", e);
            return null;
        }

    }

    public static Command getCommand(Integer id) {
        Command[] commands = values();
        for (Command command : commands) {
            if (command.getId() == id)
                return command;
        }
        return null;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Class<? extends Task> getClazz() {
        return clazz;
    }

    public void setClazz(Class<? extends Task> clazz) {
        this.clazz = clazz;
    }

    public static Logger getLog() {
        return log;
    }

    public String getInput() {
        return input;
    }

    public void setInput(String input) {
        this.input = input;
    }

    public Object getOutput() {
        return output;
    }

    public void setOutput(Object output) {
        this.output = output;
    }

    public YouyunMap getParameters() {
        return parameters;
    }

    public void setParameters(YouyunMap parameters) {
        this.parameters = parameters;
    }

    public IoSession getSession() {
        return session;
    }

    public void setSession(IoSession session) {
        this.session = session;
    }

    public HttpExchange getExchange() {
        return exchange;
    }

    public void setExchange(HttpExchange exchange) {
        this.exchange = exchange;
    }

    public Integer getTotal() {
        return total;
    }

    public void setTotal(Integer total) {
        this.total = total;
    }

    public Page getPage() {
        return page;
    }

    public void setPage(Page page) {
        this.page = page;
    }

}
